<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of "myWiWall".
# Copyright (c) 2007-2008 CRP Henri Tudor and contributors.
# All rights reserved.
#
# "myWiWall" is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
# 
# "myWiWall" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with "myWiWall"; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ***** END LICENSE BLOCK *****


/**
* @package mywiwall
* @author Laurent Haan <laurent.haan@gmail.com>
*/

?>
<h1><?php echo __("Widgets Management"); ?></h1>

<form class="add_form" id="install_widget" enctype="multipart/form-data" action="../../index.php/admin/install" method="post">
	<fieldset>
		<legend><?php echo __("Install a new widget"); ?></legend>
		
		<input name="action" type="hidden" value="install"/>
		<div class="form_line"><input name="widget" class="file" id="widget_file" type="file"/></div>
		<div class="form_line">
			<label for="category"><?php echo __("Category"); ?> : </label> <select name="category" id="category"><option value="0"><?php echo __('None'); ?></option>
		
			<?php foreach($categories as $category): ?>
			<?php printf("<option value='%s'>%s</option>", $category['id'], utf8_encode($category['category'])); ?>
			<?php endforeach;?>
		
			</select>
		</div>
		
		<div class="submit">
		<input class="button" type="submit" value="<?php echo __("Install"); ?>"/>
		<input class="button" type="reset" value="<?php echo __("Cancel"); ?>"/>
		</div>
	</fieldset>
</form>

<?php 
	$i = 0;
	$j = 0;
	while($j < count($installedWidgets)):
	
		$currentCategory = $installedWidgets[$j]['category']; ?>
	
	<h2><?php echo ($currentCategory) ? $currentCategory : __("Widgets without category"); ?></h2>
	
<?php	while ($j < count($installedWidgets) && $currentCategory == $installedWidgets[$j]['category']): 
			$widget = $installedWidgets[$j];		
?>
	<div class="item <?php echo (($i++) % 2 == 0) ? 'even' : 'odd' ?>">
		<div class="name"><?php echo htmlspecialchars($widget['name']) ?></div>
		<div class="control">
			<a href=# id="v_<?php echo $widget['id'] ?>" class="visibleOption control" title="<?php echo __("Show/Hide this Widget"); ?>"><img src="../../views/<?php echo $GLOBALS['dir_theme'] ?>img/admin-widgets-<?php echo ($widget['visible']) ? 'visible' : 'invisible' ?>.png" alt="<?php echo __("Show/Hide this widget"); ?>" class="controlImage" /></a>
			<a href=# id="a_<?php echo $widget['id'] ?>" class="authenticationOption control" title="<?php echo __("Authentication"); ?>"><img src="../../views/<?php echo $GLOBALS['dir_theme'] ?>img/admin-widgets-<?php echo ($widget['authentication']['use']) ? 'key' : 'greykey' ?>.png" alt="<?php echo __("Authentication"); ?>" class="controlImage" /></a>
			<a href=# id="e_<?php echo $widget['id'] ?>" class="editOption control" title="<?php echo __("Edit Widget"); ?>"><img src="../../views/<?php echo $GLOBALS['dir_theme'] ?>img/admin-widgets-edit.png" alt="<?php echo __("Edit widget"); ?>" class="controlImage"/></a>
			<a href=# id="d_<?php echo $widget['id'] ?>" class="deleteOption control" title="<?php echo __("Delete Widget"); ?>"><img src="../../views/<?php echo $GLOBALS['dir_theme'] ?>img/admin-widgets-delete.png" alt="<?php echo __("Delete widget"); ?>" class="controlImage" /></a>
		</div>
	</div>
	
	<div id="visibilityContainer_<?php echo $widget['id'] ?>" class="visibility container" style="display: none;">
		
		<form class="form_container" action="../../index.php/admin/toggleVisibility" method="post">
		<div class="edit_message"><?php printf(__("Are you sure you want to make the widget '%s' %s ?"), htmlspecialchars($widget['name']), ($widget['visible']) ? __('invisible') : __('visible')); ?></div>
		<input name="action" type="hidden" value="visibility"/>
		<input name="widget" type="hidden" value="<?php echo $widget['id'] ?>"/>
		
		<input class="button" type="submit" value="<?php echo __("Yes"); ?>"/>
		<input class="button" type="reset" value="<?php echo __("Cancel"); ?>"/>
		
		</form>
	</div>
	
	<div id="authenticationContainer_<?php echo $widget['id'] ?>" class="authentication container" style="display: none;">
		<form class="form_container" action="../../index.php/admin/<?php echo (!$widget['authentication']['use']) ? 'activateAuth' : 'deactivateAuth'; ?>" method="post">
		
		<?php if ($widget['authentication']['use']): ?>
		<div class="edit_message"><?php printf(__("Widget authentication system for widget '%s'."), htmlspecialchars($widget['name'])); ?></div>
		<div class="edit_message"><?php echo __("Secret key"); ?>: <?php echo base64_encode($widget['authentication']['key']); ?></div>
		<?php else: ?>
		<div class="edit_message"><?php printf(__("Widget authentication system for widget '%s' is not enabled."), htmlspecialchars($widget['name'])); ?></div>
		<?php endif; ?>
		
		<input class="button" type="reset" value="<?php echo __("Close"); ?>"/>
		</form>
	</div>
	
	<div id='editContainer_<?php echo $widget['id'];?>' class='edit container' style='display: none;'>
		<form class="form_container" enctype="multipart/form-data" action="../../index.php/admin/edit" method="post">
		<div class="edit_message"><?php printf(__("Upload new widget or manifest for widget '%s'."), htmlspecialchars($widget['name'])); ?>:</div>
		<input name="action" type="hidden" value="edit"/>
		<input name="widget" type="hidden" value="<?php echo $widget['id'] ?>"/>
		<div class="form_line"><input name="widgetFile" type="file"/></div>
		<div class="form_line"><label><?php echo __("Category"); ?></label>: <select name="category"><option value="0"><?php echo __("None"); ?></option>
		
		<?php foreach($categories as $category): ?>
		<?php printf("<option value='%s'>%s</option>", $category['id'], utf8_encode($category['category'])); ?>
		<?php endforeach;?>
		
		</select></div>
		<input class="button" type="submit" value="<?php echo __("Edit"); ?>"/>
		<input class="button" type="reset" value="<?php echo __("Cancel"); ?>"/>
		</form>
	</div>
	
	<div id="deleteContainer_<?php echo $widget['id'] ?>" class="delete container" style="display: none;">
		<form class="form_container" action="../../index.php/admin/delete" method="post">
		<div class="form_line"><?php printf(__("Are you sure you want to delete the widget '%s' ?"), htmlspecialchars($widget['name'])); ?></div>
		
		<input name="action" type="hidden" value="delete"/>
		<input name="widget" type="hidden" value="<?php echo $widget['id'] ?>"/>
		<input class="button" type="submit" value="<?php echo __('Delete'); ?>"/>
		<input class="button" type="reset" value="<?php echo __('Cancel'); ?>"/>
		</form>
	</div>
	
	<div id="integrateContainer_<?php echo $widget['id'] ?>" class="integrate container" style="display: none;">
		<div class="form_line"><?php printf(__("To integrate the widget '%s' in another website, copy the HTML code below."), htmlspecialchars($widget['name'])); ?>:</div>
		<textarea cols="80"><?php
		echo htmlspecialchars('<iframe src="http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']).'/external.php?'.$widget['id'] .'"/>');
		?></textarea>
	</div>
	
	<div id="subscribeContainer_<?php echo $widget['id'] ?>" class="integrate container" style='display: none;'>
		<div class="form_line"><?php printf(__("To create a subscription link to the widget '%s', copy the HTML source code below."), htmlspecialchars($widget['name'])); ?>:</div>
		<textarea cols="80"><?php echo htmlspecialchars('<a href="http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['REQUEST_URI']).'/subscribe.php?'.$widget['id'] .'">'. __('Subscribe') .'</a>') ?></textarea>
	</div>
	<?php $j++; endwhile; ?>
<?php endwhile; ?>